<?php
/**

 */
apf_require_class('Oa_Job_Abstract');
apf_require_class('Const_BaseInfo');
apf_require_class('Bll_Hr_OutInterface');

class Oa_Job_Portal_ExportActiveData extends Oa_Job_Abstract {
    private $read_db = "hroa_master";//读数据库
    private $write_db = "portal_master";//写数据库
    private $attendance_db = "attendance_master";

    public function run() {
        $start = 0;
        $limit = 100;
        $result = array();
        while(true) {
            $sql = "SELECT user_id,name,user_type,user_type_date,enter_date,city_name,department_id,department_name FROM user_info WHERE user_id > {$start} AND job_status='在职' ORDER BY user_id asc limit {$limit}";
            $tmp = $this->get_db_rows($sql,$this->attendance_db,array());
            if(empty($tmp)) {
                $this->log("取完所有用户数据!");
                break;
            }

            $count = count($tmp);
            $start = $tmp[$count - 1]['user_id'];
            $result = array_merge($result,$tmp);
        }
        $sql2 = "SELECT * FROM message WHERE 1 ";

        $tmp = $this->get_db_rows($sql2,$this->write_db,array());
        $act_data = array();
        if(!empty($tmp)){
            foreach($tmp as $val){
                $act_data[$val['user_id']] = $val;
            }
        }

        $kee_file = "/tmp/active2013.txt";
        $title = "ID,员工名,部门,时间,祝福语\n";
        file_put_contents($kee_file, print_r($title,true)."\n",FILE_APPEND);
        foreach($result as $val){
            if(!empty($act_data[$val['user_id']])){
                $t = $act_data[$val['user_id']]['created'];
                $c = $act_data[$val['user_id']]['content'];
                $content = $val['user_id'].",".$val['name'].",".$val['city_name'].$val['department_name'].",".$t.",".$c."\n";
                file_put_contents($kee_file, print_r($content,true),FILE_APPEND);
            }
        }
        $this->log("完成统计，请查看文件/data1/logs/v2/group/scws.csv");
    }

}